﻿using Avalonia;
using Avalonia.Controls;
using Toolkit.Lib.iOSModels;

namespace iOSClub.Toolkit.WindowApp.Controls;

public partial class UpdateMemberDataControl : UserControl
{
    private iOSMemberModel Model { get; set; }

    public UpdateMemberDataControl(iOSMemberModel model)
    {
        InitializeComponent();
        Model = model;
    }

    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);
        NameBox.Text = Model.UserName;
        IdBox.Text = Model.UserId;
        AcademyBox.ItemsSource = SignRecord.Academies;
        AcademyBox.SelectedItem = Model.Academy;
        LandscapeBox.ItemsSource = SignRecord.PoliticalLandscapes;
        LandscapeBox.SelectedItem = Model.PoliticalLandscape;
        GenderBox.ItemsSource = SignRecord.Genders;
        GenderBox.SelectedItem = Model.Gender;
        ClassBox.Text = Model.ClassName;
        PhoneNumBox.Text = Model.PhoneNum;
        IdentityBox.ItemsSource = SignRecord.IdentityDictionary.Values;
        IdentityBox.SelectedItem = SignRecord.IdentityDictionary[Model.Identity];
    }

    public (SignModel? model, PermissionsModel? permission)? Done()
    {
        if (string.IsNullOrEmpty(NameBox.Text) || string.IsNullOrEmpty(IdBox.Text) ||
            AcademyBox.SelectedItem == null || LandscapeBox.SelectedItem == null || GenderBox.SelectedItem == null ||
            string.IsNullOrEmpty(ClassBox.Text) || string.IsNullOrEmpty(PhoneNumBox.Text))
            return default;

        return (new SignModel
        {
            UserId = IdBox.Text,
            UserName = NameBox.Text,
            Academy = AcademyBox.SelectedItem.ToString()!,
            ClassName = ClassBox.Text,
            Gender = GenderBox.SelectedItem.ToString()!,
            PoliticalLandscape = LandscapeBox.SelectedItem.ToString()!,
            PhoneNum = PhoneNumBox.Text
        }, IdentityBox.Text == Model.Identity? default:new PermissionsModel()
        {
            Identity = IdentityBox.SelectedItem!.ToString()!,
            Name = NameBox.Text,
            UserId = IdBox.Text
        });
    }
}